package com.feinno.extas.app.util;

import com.feinno.extas.app.cache.PublicRedisPipeline;
import org.helium.redis.sentinel.RedisKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Response;

/**
 * 初始化redis
 *
 * @author ZKL
 */
public class RedisLockInit {
    private final static Logger LOGGER = LoggerFactory.getLogger(RedisLockInit.class);
    private final static String CACHE_NAME = "PublicPlatform-PublicAccountInfo";
    private static RedisKey redisKey = new RedisKey("LOCK");

    public static void init() {
        try {
            RedisLockUtil.setRedisCache(new RedisLockInit.RedisCacheMe());
        } catch (Exception e) {
            LOGGER.error("redis 分布式锁初始化失败", e);
        }
    }

    public static class RedisCacheMe implements RedisLockUtil.RedisCache {
        @Override
        public long setnx(String k, String v) {
            PublicRedisPipeline pipeline = null;
            long value = 0;
            try {
                if (hasEmpty(k, v)) {
                    return 0;
                }
                pipeline = new PublicRedisPipeline(CACHE_NAME, redisKey);
                Response<Long> rp = pipeline.setnx(k.getBytes(), v.getBytes());
                pipeline.sync();
                Long valueBytes = rp.get();
                value = valueBytes == null ? 0 : valueBytes;
            } catch (Exception e) {
                LOGGER.error(String.format("setnx(%s,%s,%s) error,", k, redisKey, v), e);
            } finally {
                if (pipeline != null) {
                    pipeline.close();
                }
            }
            return value;
        }

        @Override
        public String set(String k, String v, long seconds) {
            PublicRedisPipeline pipeline = null;
            String value = "";
            try {
                if (hasEmpty(k, v)) {
                    return "";
                }
                pipeline = new PublicRedisPipeline(CACHE_NAME, redisKey);
                Response<String> rp = pipeline.set(k.getBytes(), v.getBytes(), "NX".getBytes(), "PX".getBytes(), seconds);
                pipeline.sync();
                String valueBytes = rp.get();
                value = valueBytes == null ? "" : valueBytes;
            } catch (Exception e) {
                LOGGER.error(String.format("setnx(%s,%s,%s) error,", k, redisKey, v), e);
            } finally {
                if (pipeline != null) {
                    pipeline.close();
                }
            }
            return value;
        }

        @Override
        public String getSet(String k, String v) {
            PublicRedisPipeline pipeline = null;
            String value = null;
            try {
                if (hasEmpty(k, v)) {
                    return null;
                }
                pipeline = new PublicRedisPipeline(CACHE_NAME, redisKey);
                Response<byte[]> rp = pipeline.getSet(k.getBytes(), v.getBytes());
                pipeline.sync();
                byte[] valueBytes = rp.get();
                value = valueBytes == null ? "" : new String(valueBytes);
            } catch (Exception e) {
                LOGGER.error(String.format("getSet(%s,%s,%s) error,", k, redisKey, v), e);
            } finally {
                if (pipeline != null) {
                    pipeline.close();
                }
            }
            return value;
        }

        @Override
        public String get(String k) {
            PublicRedisPipeline pipeline = null;
            String value = null;
            try {
                if (hasEmpty(k)) {
                    return null;
                }
                pipeline = new PublicRedisPipeline(CACHE_NAME, redisKey);
                Response<byte[]> rp = pipeline.get(k.getBytes());
                pipeline.sync();
                byte[] valueBytes = rp.get();
                value = valueBytes == null ? null : new String(valueBytes);
            } catch (Exception e) {
                LOGGER.error(String.format("get(%s,%s) error,", k, redisKey), e);
            } finally {
                if (pipeline != null) {
                    pipeline.close();
                }
            }
            return value;
        }

        @Override
        public long del(String k) {
            PublicRedisPipeline pipeline = null;
            long value = 0;
            try {
                if (hasEmpty(k)) {
                    return 0;
                }
                pipeline = new PublicRedisPipeline(CACHE_NAME, redisKey);
                Response<Long> rp = pipeline.del(k.getBytes());
                pipeline.sync();
                Long valueBytes = rp.get();
                value = valueBytes == null ? 0 : valueBytes;
            } catch (Exception e) {
                LOGGER.error(String.format("del(%s,%s) error,", k, redisKey), e);
            } finally {
                if (pipeline != null) {
                    pipeline.close();
                }
            }
            return value;
        }

        public boolean hasEmpty(String... ss) {
            boolean isEmpty = false;
            for (String s : ss) {
                isEmpty = s == null || s.length() == 0 || "null".equals(s.trim());
                if (isEmpty) {
                    break;
                }
            }
            return isEmpty;
        }
    }
}
